| Total Complexity | 2 |
| Total Lines | 33 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | import { CommandHandler } from '@nestjs/cqrs'; |
||
| 9 | |||
| 10 | @CommandHandler(CreateUserCommand) |
||
| 11 | export class CreateUserCommandHandler { |
||
| 12 | constructor( |
||
| 13 | @Inject('IUserRepository') |
||
| 14 | private readonly userRepository: IUserRepository, |
||
| 15 | @Inject('IPasswordEncoder') |
||
| 16 | private readonly passwordEncoder: IPasswordEncoder, |
||
| 17 | private readonly isEmailAlreadyExist: IsEmailAlreadyExist |
||
| 18 | ) {} |
||
| 19 | |||
| 20 | public async execute(command: CreateUserCommand): Promise<string> { |
||
| 21 | const { firstName, lastName, password, role } = command; |
||
| 22 | const email = command.email.toLowerCase(); |
||
| 23 | |||
| 24 | if (true === (await this.isEmailAlreadyExist.isSatisfiedBy(email))) { |
||
| 25 | throw new EmailAlreadyExistException(); |
||
| 26 | } |
||
| 27 | |||
| 28 | const hashPassword = await this.passwordEncoder.hash(password); |
||
| 29 | const apiToken = await this.passwordEncoder.hash(email + password); |
||
| 30 | const user = await this.userRepository.save( |
||
| 31 | new User( |
||
| 32 | firstName, |
||
| 33 | lastName, |
||
| 34 | email, |
||
| 35 | apiToken, |
||
| 36 | hashPassword, |
||
| 37 | role |
||
| 38 | ) |
||
| 39 | ); |
||
| 40 | |||
| 41 | return user.getId(); |
||
| 42 | } |
||
| 44 |